var App = function () {
	 var isIE8 = false; // IE8 mode
	// this function handles responsive layout on screen size resize or mobile device rotate.
	    var handleResponsive = function () {
	        if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) == 8) {
	            isIE8 = true; // checkes for IE8 browser version
	            $('.visible-ie8').show(); //
	        }

	        var isIE10 = !! navigator.userAgent.match(/MSIE 10/);

	        if (isIE10) {
	            jQuery('html').addClass('ie10'); // set ie10 class on html element.
	        }
	    }
	    
	    var handleMainMenu = function () {
	        jQuery('.page-sidebar .has-sub > a').click(function () {
	            var last = jQuery('.has-sub.open', $('.page-sidebar'));
	            last.removeClass("open");
	            jQuery('.arrow', last).removeClass("open");
	            jQuery('.sub', last).slideUp(200);
	            var sub = jQuery(this).next();
	            if (sub.is(":visible")) {
	                jQuery('.arrow', jQuery(this)).removeClass("open");
	                jQuery(this).parent().removeClass("open");
	                sub.slideUp(200);
	            } else {
	                jQuery('.arrow', jQuery(this)).addClass("open");
	                jQuery(this).parent().addClass("open");
	                sub.slideDown(200);
	            }
	        });
	    }
	    
	    var handleSubMenu = function () {
	        jQuery('.page-sidebar .has-sub .sub li > a').click(function () {
	            var last = jQuery('.sub li.active', $('.page-sidebar .has-sub'));
	            last.removeClass("active");
	            jQuery(this).parent().addClass("active");
	            
	            var title=jQuery(this).parent().parent().parent().first().find(".title").text();
	            var subTitle=jQuery(this).text();
	            var href=jQuery(this).attr("href");
	            var str='<li class="remo"><a>'+title+'</a><span class="icon-angle-right"></span></li>';
	            $("#page-title").text(subTitle);	           
	            $("#breadcrumb .remo").remove();	            
	            $("#breadcrumb > li:first").after(str);
	            $("#breadcrumb > li:last > a").attr("href",href).text(subTitle);
	            
	            jQuery('.start.active', $('.page-sidebar')).removeClass("active");
	            jQuery('.has-sub.active', $('.page-sidebar')).removeClass("active");
	            jQuery('.has-sub.open', $('.page-sidebar')).addClass("active");	            
	            jQuery('.has-sub > a .selected', $('.page-sidebar')).remove();	          	            
	            jQuery('.has-sub.active > a', $('.page-sidebar')).first().find(".title").after('<span class="selected"></span>');
	        });
	    }
	    
	    var handleSidebarToggler = function () {

	        var container = $(".page-container");

	        if ($.cookie('sidebar-closed') == 1) {
	            container.addClass("sidebar-closed");
	        }

	        // handle sidebar show/hide
	        $('.page-sidebar .sidebar-toggler').click(function () {
	            $(".sidebar-search").removeClass("open");
	            var container = $(".page-container");
	            if (container.hasClass("sidebar-closed") === true) {
	                container.removeClass("sidebar-closed");
	                $.cookie('sidebar-closed', null);
	            } else {
	                container.addClass("sidebar-closed");
	                $.cookie('sidebar-closed', 1);
	            }
	        });

	        // handle the search bar close
	        $('.sidebar-search .remove').click(function () {
	            $('.sidebar-search').removeClass("open");
	        });

	        // handle the search query submit on enter press
	        $('.sidebar-search input').keypress(function (e) {
	            if (e.which == 13) {
	               // window.location.href = "extra_search.html";
	                return false; //<---- Add this line
	            }
	        });

	        // handle the search submit
	        $('.sidebar-search .submit').click(function () {
	            if ($('.page-container').hasClass("sidebar-closed")) {
	                if ($('.sidebar-search').hasClass('open') == false) {
	                    $('.sidebar-search').addClass("open");
	                } else {
	                    //window.location.href = "extra_search.html";
	                }
	            } else {
	                //window.location.href = "extra_search.html";
	            }
	        });
	    }
	    
	    var handleFixInputPlaceholderForIE = function () {
	        //fix html5 placeholder attribute for ie7 & ie8
	        if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) <= 9) { // ie7&ie8
	            jQuery('input[placeholder], textarea[placeholder]').each(function () {

	                var input = jQuery(this);

	                jQuery(input).val(input.attr('placeholder'));

	                jQuery(input).focus(function () {
	                    if (input.val() == input.attr('placeholder')) {
	                        input.val('');
	                    }
	                });

	                jQuery(input).blur(function () {
	                    if (input.val() == '' || input.val() == input.attr('placeholder')) {
	                        input.val(input.attr('placeholder'));
	                    }
	                });
	            });
	        }
	    }
	    
	    var handleGoTop = function () {
	        /* set variables locally for increased performance */
	        jQuery('.footer .go-top').click(function () {
	            App.scrollTo();
	        });
	    }
	    
	    var handleStyler = function () {

	        var panel = $('.color-panel');

	        $('.icon-color', panel).click(function () {
	            $('.color-mode').show();
	            $('.icon-color-close').show();
	        });

	        $('.icon-color-close', panel).click(function () {
	            $('.color-mode').hide();
	            $('.icon-color-close').hide();
	        });

	        $('li', panel).click(function () {
	            var color = $(this).attr("data-style");
	            setColor(color);
	            $('.inline li', panel).removeClass("current");
	            $(this).addClass("current");
	        });

	        $('input', panel).change(function () {
	            setLayout();
	        });

	        var setColor = function (color) {
	            $('#style_color').attr("href", "css/style_" + color + ".css");
	        }

	        var setLayout = function () {
	            if ($('input.header', panel).is(":checked")) {
	                $("body").addClass("fixed-top");
	                $(".header").addClass("navbar-fixed-top");
	            } else {
	                $("body").removeClass("fixed-top");
	                $(".header").removeClass("navbar-fixed-top");
	            }
	        }
	    }
	    
	    var handleUniform = function () {
	        if (!jQuery().uniform) {
	            return;
	        }
	        var test = $("input[type=checkbox]:not(.toggle), input[type=radio]:not(.toggle, .star)");
	        if (test) {
	            test.uniform();
	        }
	    }
	    
	    var handlePulsate = function () {
	        if (!jQuery().pulsate) {
	            return;
	        }

	        if (isIE8 == true) {
	            return; // pulsate plugin does not support IE8 and below
	        }

	        if (jQuery().pulsate) {
	            jQuery('#pulsate-regular').pulsate({
	                color: "#bf1c56"
	            });

	            jQuery('#pulsate-once').click(function () {
	                $(this).pulsate({
	                    color: "#399bc3",
	                    repeat: false
	                });
	            });

	            jQuery('#pulsate-hover').pulsate({
	                color: "#5ebf5e",
	                repeat: false,
	                onHover: true
	            });

	            jQuery('#pulsate-crazy').click(function () {
	                $(this).pulsate({
	                    color: "#fdbe41",
	                    reach: 50,
	                    repeat: 10,
	                    speed: 100,
	                    glow: true
	                });
	            });
	        }
	    };
	    
	    var handleLoginForm = function () {

	        $('.login-form').validate({
	            errorElement: 'label', //default input error message container
	            errorClass: 'help-inline', // default input error message class
	            focusInvalid: false, // do not focus the last invalid input
	            rules: {
	                username: {
	                    required: true
	                },
	                password: {
	                    required: true
	                },
	                remember: {
	                    required: false
	                }
	            },

	            invalidHandler: function (event, validator) { //display error alert on form submit   
	                $('.alert-error', $('.login-form')).show();
	            },

	            highlight: function (element) { // hightlight error inputs
	                $(element)
	                    .closest('.control-group').addClass('error'); // set error class to the control group
	            },

	            success: function (label) {
	                label.closest('.control-group').removeClass('error');
	                label.remove();
	            },

	            errorPlacement: function (error, element) {
	                error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
	            }
	        });

	        $('.login-form input').keypress(function (e) {
	            if (e.which == 13) {
	                if ($('.login-form').validate().form()) {
	                	$('.login-form').submit();
	                }
	                return false;
	            }
	        });
	    };
	    
	    
	var handleTagsInput = function() {
		if (!jQuery().tagsInput) {
			return;
		}
		$('#tags_1').tagsInput({
			width : 'auto',
			'onAddTag' : function() {
				alert(1);
			},
			
   			maxChars : 2 
		});
		$('#tags_2').tagsInput({
			width : 300,
			interactive : false,
			autocomplete_url:'fake_plaintext_endpoint.html',
  			autocomplete:{selectFirst:true,width:'100px',autoFill:true}
		});
	}

    
	    return {

	        //main function to initiate template pages
	        init: function () {
	            handleResponsive(); // set and handle responsive
	            handleTagsInput();
	            handleMainMenu(); // handles main menu
	            handleSubMenu();
	            handleSidebarToggler();// handles sidebar hide/show
	            handleFixInputPlaceholderForIE(); // fixes/enables html5 placeholder attribute for IE9, IE8
	            handleGoTop(); //handles scroll to top functionality in the footer
	            handleStyler();
	            handleUniform();
	            handlePulsate();
	        },	    

	        // login page setup
	        initLogin: function () {
	            handleLoginForm(); // handles login form
	            handleUniform(); // // handles uniform elements
	            handleFixInputPlaceholderForIE(); // fixes/enables html5 placeholder attribute for IE9, IE8
	        },
	        
	        // wrapper function to scroll to an element
	        scrollTo: function (el, offeset) {
	            pos = el ? el.offset().top : 0;
	            jQuery('html,body').animate({
	                scrollTop: pos + (offeset ? offeset : 0)
	            }, 'slow');
	        }	     

	    };
}();